package com.example.jetpackdemo.utils

import android.content.Context
import android.content.SharedPreferences
import com.example.jetpackdemo.common.BaseApplication
import com.example.jetpackdemo.common.BaseConstant

object AppPrefsUtils {
    private var sp: SharedPreferences = BaseApplication.context.getSharedPreferences(
        BaseConstant.TABLE_PREFS,
        Context.MODE_PRIVATE
    )
    private var ed: SharedPreferences.Editor

    init {
        ed = sp.edit()
    }

    /*
    Boolean数据
     */
    fun putBoolean(key: String, value: Boolean) {
        ed.putBoolean(key, value)
        ed.commit()
    }

    /*
    默认 false
     */
    fun getBoolean(key: String): Boolean {
        return sp.getBoolean(key, true)
    }

    /*
     String数据
     */
    fun putString(key: String, value: String) {
        ed.putString(key, value)
        ed.commit()
    }

    /*
    默认 ""
     */

    fun getString(key: String): String? {
        return sp.getString(key, "")
    }

    /*
    Int 数据
     */

    fun putInt(key: String, value: Int) {
        ed.putInt(key, value)
        ed.commit()
    }

    /*
    默认0
     */
    fun getInt(key: String): Int {
        return sp.getInt(key, 0)
    }


    /*
    Long 数据
     */
    fun putLong(key: String, value: Long) {
        ed.putLong(key, value)
        ed.commit()
    }

    /*
    默认 0
     */
    fun getLong(key: String): Long {
        return sp.getLong(key, 0)
    }

    /*
    Set 数据
     */
    fun putStringSet(key: String, set: Set<String>) {
        val localSet = getStringSet(key)?.toMutableSet()
        localSet?.addAll(set)
        ed.putStringSet(key, localSet)
        ed.commit()
    }

    /*
    默认空
     */

    fun getStringSet(key: String): MutableSet<String>? {
        val set = setOf<String>()
        return sp.getStringSet(key, set)
    }

    /*
    删除key数据
     */

    fun remove(key: String) {
        ed.remove(key)
        ed.commit()
    }


}